package com.okyai.qa.controller;

import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.okyai.common.annotation.Log;
import com.okyai.common.enums.BusinessType;
import com.okyai.qa.domain.Notes;
import com.okyai.qa.service.INotesService;
import com.okyai.common.core.controller.BaseController;
import com.okyai.common.core.domain.AjaxResult;
import com.okyai.common.utils.poi.ExcelUtil;
import com.okyai.common.core.page.TableDataInfo;

/**
 * 答题记录Controller
 * 
 * @author okyai
 * @date 2021-03-19
 */
@Controller
@RequestMapping("/qa/notes")
public class NotesController extends BaseController
{
    private String prefix = "qa/notes";

    @Autowired
    private INotesService notesService;

    @RequiresPermissions("qa:notes:view")
    @GetMapping()
    public String notes()
    {
        return prefix + "/notes";
    }

    /**
     * 查询答题记录列表
     */
    @RequiresPermissions("qa:notes:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Notes notes)
    {
        startPage();
        List<Notes> list = notesService.selectNotesList(notes);
        return getDataTable(list);
    }

    /**
     * 导出答题记录列表
     */
    @RequiresPermissions("qa:notes:export")
    @Log(title = "答题记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Notes notes)
    {
        List<Notes> list = notesService.selectNotesList(notes);
        ExcelUtil<Notes> util = new ExcelUtil<Notes>(Notes.class);
        return util.exportExcel(list, "notes");
    }

    /**
     * 新增答题记录
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存答题记录
     */
    @RequiresPermissions("qa:notes:add")
    @Log(title = "答题记录", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Notes notes)
    {
        return toAjax(notesService.insertNotes(notes));
    }

    /**
     * 修改答题记录
     */
    @GetMapping("/edit/{notesId}")
    public String edit(@PathVariable("notesId") Long notesId, ModelMap mmap)
    {
        Notes notes = notesService.selectNotesById(notesId);
        mmap.put("notes", notes);
        return prefix + "/edit";
    }

    /**
     * 修改保存答题记录
     */
    @RequiresPermissions("qa:notes:edit")
    @Log(title = "答题记录", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Notes notes)
    {
        return toAjax(notesService.updateNotes(notes));
    }

    /**
     * 删除答题记录
     */
    @RequiresPermissions("qa:notes:remove")
    @Log(title = "答题记录", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(notesService.deleteNotesByIds(ids));
    }
}
